package com.qall.configserver;

import com.qall.configability.entity.GetConfigRequest;
import com.qall.configability.service.DataService;
import com.qall.configserver.entity.BaseResponse;
import com.qall.configserver.entity.GetConfigResponse;
import com.qall.configserver.entity.UpdateConfigRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.Map;

@Controller
@ResponseBody
@RequestMapping("config")
public class ConfigSServerController {

    @Resource
    private DataService dataService;

    /**
     * 更新配置
     */
    @RequestMapping(path = "updateConfig", method = RequestMethod.POST)
    public BaseResponse updateConfig(@RequestBody UpdateConfigRequest request) {
        dataService.updateData(request.getNameSpace(), request.getAppId(), request.getEntityList());
        return new BaseResponse();
    }


    /**
     * 获取配置
     */
    @RequestMapping(path = "getConfig", method = RequestMethod.GET)
    public GetConfigResponse getConfig(@RequestParam(name="nameSpace") String nameSpace, @RequestParam String appName) {
        GetConfigResponse getConfigResponse = new GetConfigResponse();
        Map<String, String> map = dataService.getConfig(new GetConfigRequest(nameSpace, appName));
        getConfigResponse.setConfigMap(map);
        return getConfigResponse;
    }



}


